$(document).ready(function(){
	init();
});

function init(){
	window.tabId = "#startUpMenuOuter"; //if you change this variable change the css file for startUpMenu as well
	window.professionId = ":radio";
	var buttonId = "#continue"; 
	
	// global vars
	window.check = $('.checkbox');
	window.errorMark = $('.errorMark');
	window.errorString='<div id="error">';
	window.namePattern = /^('?([a-z]+|[A-Z]+)+(('|-)([a-z]|[A-Z]))?\s?)+$/i;
		
	setUpDefault(tabId,professionId,buttonId);
	keyListener();
	
}

function setUpDefault(tabId,professionId,buttonId)
{
	$(tabId).tabs();
	$(tabId).resizable({
		minHeight: 500,
		minWidth: 400,
		
	});
	//$('[name="profession"]').buttonset();
	$(buttonId).button();	
}

function validate(value) 
{	
	var valid=false;
	// Capital letter, any number of lowercase optional space another capital letter
	//var namePattern=/([A-Z]?[a-z]*)\\-?\\s?([A-Z]?[a-z]*)?/i;  ///([A-Z]?[a-z]*)|([A-Z]?[a-z]*\\-[A-Z]?[a-z]*)|([A-Z]?[a-z]*\\s+[A-Z]?[a-z])/i;
	
	if (value.match(namePattern) != null)  //maybe != ""
	{
		valid = true;
	}
	
	return valid;
}

function keyListener()
{
	$(':text').keypress(function(event) {
		$this = jQuery(event.target);
		if (validate($this.val())) {
			$this.next('.image').remove();
			$this.after(check.clone());
		}
		else {
			$this.next('.image').remove();
			$this.after(errorMark.clone());
		}
	});
	
	$(':text').keyup(function(event) {
		$this = jQuery(event.target);
		if (validate($this.val())) {
			$this.next('.image').remove();
			$this.after(check.clone());
		}
		else {
			$this.next('.image').remove();
			$this.after(errorMark.clone());
		}
	}); 
}

	
function professionHelper() {
	var tmpTip;
	if (radioObject.checked)
	{
		document.write(tmpTip); // tmpTip will be stored somewhere else for
	}						// improved abstraction
}

function loadIndependence() {
	$.ajax({url:'/travel/independence',
		type: "GET",
		data: '',
		async:false,
		dataType: 'html',
		success:function(info){
			$(tabId + ' > ul').append('<li><a href="#store">Choose Supplies</a></li>');
			$('#startUpMenuOuter').append(info);
		},
		error:function(){
			$.notifyBar({
				html: 'An error occured while sending the data',
				delay: 2000,
				animationSpeed: "normal",
				cls: "error"
			});
		}
	});
	
}

function submitTeamInfo($this){
	$this = jQuery($this);
	$.ajax({url:$this.attr('url'),
		type: "POST",
		data: $this.parents('form').serialize(),
		dataType: 'html',
		success:function(info){
			
			if (info.indexOf(errorString) != -1){
				$.notifyBar({
					html: 'Your form has errors, please correct them below',
					delay: 2000,
					animationSpeed: "normal",
					cls: "error"
				});
				$('#teamInfoForm *').remove();
				$('#teamInfoForm').append(info);
				init();
			} 
			else {
				$('#teamInfoForm *').remove();
				$('#teamInfoForm').append(info);
				$('#teamInfoForm :input').attr('disabled','true');
				$('#teamInfoForm :button').remove();
				$(tabId).tabs('destroy')
				loadIndependence();
				init();
				$(tabId).tabs('select',1);
				$(tabId).tabs('disable',0);
				
			}
		},
		error:function(){
			$.notifyBar({
				html: 'An error occured while sending the data',
				delay: 2000,
				animationSpeed: "normal",
				cls: "error"
			});
		}
	});
}